﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Temp.Properties;

namespace YArchitech.Temp
{
	public partial class PipeSpecificationForm : System.Windows.Forms.Form
	{
		public PipeSpecificationForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.ColumnDN.ValueRange = new MostValue(0.0, 2000.0);
			this.ColumnDiameter.ValueRange = new MostValue(0.0, 2000.0);
			this.ColumnMinV.ValueRange = new MostValue(0.0, 1000.0);
			this.ColumnMaxV.ValueRange = new MostValue(0.0, 1000.0);
			this.ColumnMinRm.ValueRange = new MostValue(0.0, 0.0);
			this.ColumnMaxRm.ValueRange = new MostValue(0.0, 0.0);
			this.ColumnMinH.ValueRange = new MostValue(0.0, 0.0);
			this.ColumnMaxH.ValueRange = new MostValue(0.0, 0.0);
		}

		public PipeSpecificationForm(HYPipeSpecifications specs, Autodesk.Revit.DB.Document document) : this()
		{
			this.m_Specifications = specs;
			this.revitDoc = document;
		}

		private void PipeSpecificationForm_Load(object sender, EventArgs e)
		{
			base.CenterToParent();
			this.btnSpecAdd.Image = YArchitech.LIB.Resources.AddNew16;
			this.btnSpecDel.Image = YArchitech.LIB.Resources.delete16;
			this.FillSpecificationGrid();
		}

		private void FillSpecificationGrid()
		{
			if (this.m_Specifications == null || this.m_Specifications.Count == 0)
			{
				return;
			}
			this.hyGridViewSpec.DeleteAllRows();
			foreach (HYPipeSpecification sp in this.m_Specifications)
			{
				DataGridViewRow dataGridViewRow = this.CreateSpecRow(sp);
				this.hyGridViewSpec.Rows.Add(dataGridViewRow);
			}
		}

		private HYPipeSpecificationRow CreateSpecRow(HYPipeSpecification sp)
		{
			HYPipeSpecificationRow hypipeSpecificationRow = new HYPipeSpecificationRow();
			hypipeSpecificationRow.CreateCells(this.hyGridViewSpec);
			hypipeSpecificationRow.Binding(sp);
			return hypipeSpecificationRow;
		}

		private void btnSpecAdd_Click(object sender, EventArgs e)
		{
			HYPipeSpecification sp = new HYPipeSpecification();
			int count = this.hyGridViewSpec.Rows.Count;
			HYPipeSpecificationRow hypipeSpecificationRow = this.CreateSpecRow(sp);
			this.hyGridViewSpec.Rows.Add(hypipeSpecificationRow);
			this.hyGridViewSpec.SelectRow(hypipeSpecificationRow);
		}

		private void btnSpecDel_Click(object sender, EventArgs e)
		{
			if (this.hyGridViewSpec.RowCount <= 1)
			{
				YJKMessageBox.Show("水管规格不能全部删除！");
				return;
			}
			if (this.m_Specifications == null || this.hyGridViewSpec.SelectedRows.Count == 0)
			{
				return;
			}
			if (DialogResult.Yes != MessageBox.Show(YArchitech.Temp.Properties.Resources.ConfirmForDelete, Resource.MessageBoxTitle, MessageBoxButtons.YesNo))
			{
				return;
			}
			for (int i = this.hyGridViewSpec.SelectedRows.Count - 1; i > -1; i--)
			{
				HYPipeSpecificationRow hypipeSpecificationRow = this.hyGridViewSpec.SelectedRows[i] as HYPipeSpecificationRow;
				HYPipeSpecification spec = hypipeSpecificationRow.GetSpec();
				this.m_Specifications.Remove(spec);
				this.hyGridViewSpec.Rows.Remove(hypipeSpecificationRow);
				new ManagePipeSpecification(this.revitDoc).DeleteMepSize((double)spec.DN / 304.8);
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			return base.ProcessCmdKey(ref msg, keyData);
		}

		private void PipeSpecificationForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (Convert.ToInt32(e.KeyChar) == 27)
			{
				base.DialogResult = DialogResult.Cancel;
			}
		}

		private void hyGridViewSpec_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			int rowIndex = e.RowIndex;
			int columnIndex = e.ColumnIndex;
			if (rowIndex >= 0)
			{
			}
		}

		private void hyGridViewSpec_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			int rowIndex = e.RowIndex;
			int columnIndex = e.ColumnIndex;
			if (rowIndex < 0 || columnIndex < 0)
			{
				return;
			}
			if (e.FormattedValue == null)
			{
				YJKMessageBox.Show("不能为空！");
				e.Cancel = true;
			}
			if (!Regex.IsMatch(e.FormattedValue.ToString(), "^\\d+(\\.\\d+)?$") || e.FormattedValue.ToString() == "0")
			{
				YJKMessageBox.Show("填入数字应为正数！");
				e.Cancel = true;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_Specifications.Clear();
			foreach (object obj in ((IEnumerable)this.hyGridViewSpec.Rows))
			{
				HYPipeSpecification spec = (((DataGridViewRow)obj) as HYPipeSpecificationRow).GetSpec();
				if (spec.IsValid)
				{
					this.m_Specifications.Add(spec);
				}
			}
			HYDataAccess.SavePipeSpecifications(this.m_Specifications);
			base.DialogResult = DialogResult.OK;
		}

		private void hyGridViewSpec_Paint(object sender, PaintEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			using (SolidBrush solidBrush = new SolidBrush(dataGridView.RowHeadersDefaultCellStyle.ForeColor))
			{
				e.Graphics.DrawString("序号", dataGridView.Font, solidBrush, 4f, 10f);
			}
		}

		private void hyGridViewSpec_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
		{
			using (SolidBrush solidBrush = new SolidBrush(((DataGridView)sender).RowHeadersDefaultCellStyle.ForeColor))
			{
				e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, solidBrush, (float)(e.RowBounds.Location.X + 13), (float)(e.RowBounds.Location.Y + 7));
			}
		}

		private void btnSpecDel_MouseEnter(object sender, EventArgs e)
		{
			this.beCheck = true;
		}

		private void btnSpecDel_MouseLeave(object sender, EventArgs e)
		{
			this.beCheck = false;
		}

		private void btnCancel_MouseEnter(object sender, EventArgs e)
		{
			this.beCheck = true;
		}

		private void btnCancel_MouseLeave(object sender, EventArgs e)
		{
			this.beCheck = false;
		}

		private HYPipeSpecifications m_Specifications;

		private Document revitDoc;

		private bool beCheck;
	}
}
